package xjtu.se.articlecommunity.common.SSE.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.se.articlecommunity.common.SSE.dto.MessNum;
import xjtu.se.articlecommunity.common.SSE.service.MessService;
import xjtu.se.articlecommunity.common.SSE.MessageType;
import xjtu.se.articlecommunity.common.SSE.service.SSEService;
import xjtu.se.articlecommunity.common.SSE.dao.MessDao;
import xjtu.se.articlecommunity.common.SSE.dto.Mess;
import xjtu.se.articlecommunity.common.SSE.dto.MessShow;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@Service
@Transactional
public class MessServiceImpl implements MessService {

    @Resource
    private SSEService sseService;

    @Resource
    private MessDao messDao;

    private Mess getInfo(MessageType type, String sendId, String id) {
        Mess mess = new Mess();
        mess.setM_id(System.currentTimeMillis() + sendId);
        mess.setU_send_id(sendId);

        if (type.equals(MessageType.article_like)) {
            mess.setM_type("1");
            mess.setA_id(id);
            mess.setC_id(null);
            mess.setU_receive_id(messDao.getArticleAuthor(id));
        } else if (type.equals(MessageType.comment_like)) {
            mess.setM_type("2");
            mess.setC_id(id);
            mess.setA_id(messDao.getAByC(id));
            mess.setU_receive_id(messDao.getCommentAuthor(id));
        } else if (type.equals(MessageType.comment)) {
            mess.setM_type("3");
            mess.setC_id(id);
            mess.setA_id(messDao.getAByC(id));
            mess.setU_receive_id(messDao.getArticleAuthor(mess.getA_id()));
        } else if (type.equals(MessageType.reply)) {
            mess.setM_type("4");
            mess.setC_id(id);
            mess.setA_id(messDao.getAByC(id));
            mess.setU_receive_id(messDao.getReplyTarget(id));
        } else {
            mess.setM_type("5");
            mess.setC_id(null);
            mess.setA_id(id);
            mess.setU_receive_id(messDao.getArticleAuthor(id));
            if(type.equals(MessageType.disapprove)) mess.setM_type("6");
        }

        return mess;
    }

    @Override
    public void sendMessage(MessageType type, String sendId, String id) throws IOException {
        Mess mess = getInfo(type, sendId, id);
        messDao.insert(mess);
        sseService.push(mess.getU_receive_id(), mess.getM_type());
    }

    @Override
    public List<MessShow> getLikesList(String u_id) {
        return messDao.getLikesList(u_id,"1", "2");
    }

    @Override
    public List<MessShow> getCommentList(String u_id) {
        return messDao.getCommentList(u_id, "3");
    }

    @Override
    public List<MessShow> getReplyList(String u_id) {
        return messDao.getCommentList(u_id, "4");
    }

    @Override
    public List<MessShow> getApproveList(String u_id) {
        return messDao.getApproveList(u_id, "5", "6");
    }

    @Override
    public MessNum getNewMessageNumber(String u_id) {
        MessNum messNum = new MessNum();
        List<MessShow> list;
        int num;
        int all = 0;

        //comment
        num = 0;
        list = getCommentList(u_id);
        for (MessShow mess : list)
            if (mess.getIfChecked().equals("0"))
                num++;
        all += num;
        messNum.setComment(num);

        //reply
        num = 0;
        list = getReplyList(u_id);
        for (MessShow mess : list)
            if (mess.getIfChecked().equals("0"))
                num++;
        all += num;
        messNum.setReply(num);

        //likes
        num = 0;
        list = getLikesList(u_id);
        for (MessShow mess : list)
            if (mess.getIfChecked().equals("0"))
                num++;
        all += num;
        messNum.setLikes(num);

        //sys
        num = 0;
        list = getApproveList(u_id);
        for (MessShow mess : list)
            if (mess.getIfChecked().equals("0"))
                num++;
        all += num;
        messNum.setSys(num);

        messNum.setAll(all);
        return messNum;
    }

    @Override
    public void clear(String messType1, String messType2, String u_id) {
        messDao.clear(messType1, messType2, u_id);
    }
}